package gojs;

import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;
import velocity.AsyncFlowVelocity;
import velocity.VelocityModel;
import velocity.VelocityRequest;

import java.util.LinkedList;
import java.util.stream.Collectors;

@RestController
@RequestMapping("asyncFlowCode")
public class BuildAsyncFlowCodeController {


    @SneakyThrows
    @PostMapping("buildAsyncFlowCodeDemo")
    public void buildAsyncFlowCodeDemo() {
        String AsyncFlowModuleName = "AddResource"; //模块名称【英文描述一个单词比如合同-contract】
        String AsyncFlowModuleNameDes = "新增子资源";//节点描述
        String AsyncFlowMaintainName = "07612|14856"; //异步流责任人名称
        int AsyncFlowTimeOutsLimit = 5000;   //异步流超时报警时间(毫秒)

        LinkedList<String[]> nodeLinkedList = new LinkedList<>();
        nodeLinkedList.add(new String[]{"Validator", "AddResourceBaseValidator", "false", "新增子资源基础验证"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_CheckProductOperator", "false", "产品信息验证"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddSingleOperator", "false", "新增单品资源"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddAppointmentOperator", "false", "酒景预约产品新增预约单信息"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddPassengerOperator", "false", "酒景预约产品新增入住人信息"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_AddExtraChargeOperator", "false", "新增附加资源"});
        nodeLinkedList.add(new String[]{"Operator", "AddResource_UpOrderInfoOperator", "false", "更新订单信息（价格、支付状态）"});
        nodeLinkedList.add(new String[]{"Sync", "AddResource_CallStockSync", "false", "调用库存占位接口"});

        VelocityRequest velocityRequest = new VelocityRequest();
        for (String[] node : nodeLinkedList) {
            VelocityModel.AsyncFlowNodeInfo asyncFlowNodeInfo = new VelocityModel.AsyncFlowNodeInfo();
            asyncFlowNodeInfo.setExecuteName(node[0]);
            asyncFlowNodeInfo.setAsyncFlowNodeFileName(node[1]);
            asyncFlowNodeInfo.setAsync(Boolean.parseBoolean(node[2]));
            asyncFlowNodeInfo.setAsyncFlowNodeDes(node[3]);
            velocityRequest.getAsyncFlowNodeInfoList().add(asyncFlowNodeInfo);
        }
        //模块名称【英文描述一个单词比如合同-contract】
        velocityRequest.setAsyncFlowModuleName(AsyncFlowModuleName);
        //节点描述
        velocityRequest.setAsyncFlowModuleDes(AsyncFlowModuleNameDes);
        //异步流责任人名称
        velocityRequest.setAsyncFlowMaintainName(AsyncFlowMaintainName);
        //异步流超时报警时间(毫秒)
        velocityRequest.setAsyncFlowTimeOutsLimit(AsyncFlowTimeOutsLimit);
        velocityRequest.setHasBaseContext(true);
        AsyncFlowVelocity.buildAsyncFlowVelocity(velocityRequest);

    }


    @SneakyThrows
    @PostMapping("buildAsyncFlowCodeV2")
    public String buildAsyncFlowCodeV2(@NotNull @RequestBody VelocityRequest velocityRequest) {
        String result = "生成成功";
        velocityRequest.setHasBaseContext(true);
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowModuleName())) {
            result = "异步流模块英文名不能为空";
            return result;
        }
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowModuleDes())) {
            result = "异步流中文描述不能为空";
            return result;
        }
        if (StringUtils.isBlank(velocityRequest.getAsyncFlowMaintainName())) {
            result = "异步流负责人";
            return result;
        }
        if (velocityRequest.getAsyncFlowTimeOutsLimit() <= 0) {
            result = "异步流超时时间必须大于0";
            return result;
        }
        velocityRequest.getAsyncFlowNodeInfoList().removeAll(velocityRequest.getAsyncFlowNodeInfoList().stream().filter(c -> StringUtils.isBlank(c.getAsyncFlowNodeFileName())).collect(Collectors.toList()));
        if (velocityRequest.getAsyncFlowNodeInfoList().isEmpty()) {
            result = "至少包含一个节点";
            return result;
        }
        AsyncFlowVelocity.buildAsyncFlowVelocity(velocityRequest);
        return result;

    }

    @SneakyThrows
    @PostMapping("loadPubNodeManager")
    public LinkedList<VelocityModel.AsyncFlowNodeInfo> loadPubNodeManager() {
        LinkedList<VelocityModel.AsyncFlowNodeInfo> pubAsyncFlowNodeInfoList;
        //com.tcgl.eorderservice.orderflow.Manager.contract
        //com.tcgl.eorderservice.orderflow.GetData.pubgetdata
        String loadPubNodePackPath = "com.tcgl.eorderservice.orderflow.GetData.pubgetdata";
        pubAsyncFlowNodeInfoList = AsyncFlowVelocity.loadPubNode("GetDataPub", loadPubNodePackPath);
        String loadManagerPackPath = "com.tcgl.eorderservice.orderflow.Manager";
        pubAsyncFlowNodeInfoList.addAll(AsyncFlowVelocity.loadPubNode("Manager", loadManagerPackPath));
        return pubAsyncFlowNodeInfoList;

    }

    @SneakyThrows
    @GetMapping("loadManagerNode")
    public VelocityRequest loadManagerNode(@RequestParam("asyncFlowModuleName") String asyncFlowModuleName) {
        LinkedList<VelocityModel.AsyncFlowNodeInfo> pubAsyncFlowNodeInfoList;
        //com.tcgl.eorderservice.orderflow.Manager.contract
        //com.tcgl.eorderservice.orderflow.GetData.pubgetdata
        String loadManagerNodePackPath = "com.tcgl.eorderservice.orderflow.Manager";
        return AsyncFlowVelocity.loadManagerNode(asyncFlowModuleName, loadManagerNodePackPath);

    }
}
